import sys
from PyQt5.QtWidgets import QApplication

from PyQt5.QtWidgets import (
    QMainWindow, QWidget, QAction, qApp,
    QVBoxLayout)
from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication,pyqtSlot
from PyQt5.QtWidgets import QPushButton,QLabel

class MySignal(QObject):
    # 定义信号，信号有两个参数，两个参数的类型分别为str,str，信号名称为dataChanged
    custom_sign = pyqtSignal(int)
class MyPushButton(QPushButton):
    myIndex = 0
    mySignal = None
    def __init__(self,*__args):
        super(MyPushButton, self).__init__()
        self.mySignal = MySignal()
    # def clicked(self, checked=False):
    #     super(MyPushButton,self).clicked(checked)

    def onMyClicked(self,checked):
        print(checked,self.myIndex)

        self.mySignal.custom_sign.emit(self.myIndex)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()
    def onTopBtn(self,checked):
        print("onTopBtn",checked)


    def init_ui(self):
        # 设置窗口基本信息
        self.resize(800, 600)
        self.setWindowTitle("demo1")
        # 设置菜单
        self.init_menu_ui()
        # 设置主界面
        view = QWidget()
        # view.setStyleSheet("background-color:#FFFFF0;")
        v_layout = QVBoxLayout()
        v_layout.setContentsMargins(20,0,20,0)
        for index in range(10):
            topBtn = MyPushButton(self)
            topBtn.myIndex = index
            topBtn.mySignal.custom_sign.connect(self.onClickedIndex)
            topBtn.setText("topButton %d"%index)

            topBtn.clicked.connect(topBtn.onMyClicked)
            v_layout.addWidget(topBtn)

        view.setLayout(v_layout)
        self.setCentralWidget(view)

        # 显示
        self.show()
    def onClickedIndex(self,index):
        print("主窗口接收：",index)
    def init_menu_ui(self):
        # 设置菜单项
        menubar = self.menuBar()
        # menubar.setNativeMenuBar(False)
        marketMenu = menubar.addMenu('文件')
        userMenu = menubar.addMenu("打开")
        aboutMenu = menubar.addMenu("保存")

        dataAction = QAction("编辑", self)
        downloadAction = QAction("替换", self)
        marketMenu.addAction(dataAction)
        marketMenu.addAction(downloadAction)

        usernameAction = QAction('username', self)
        logoutAction = QAction('退出登录', self)
        logoutAction.setStatusTip('退出登录')
        userMenu.addAction(usernameAction)
        userMenu.addAction(logoutAction)

        # 给menu创建一个Action
        helpAction = QAction('查看帮助', self)
        helpAction.setShortcut('Ctr+Q')
        helpAction.setStatusTip('查看帮助')

        # 将这个Action添加到fileMenu上
        aboutMenu.addAction(helpAction)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = MainWindow()

    sys.exit(app.exec_())
